package com.baizan.daka.util

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.WriterException
import com.google.zxing.common.BitMatrix
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import java.nio.charset.StandardCharsets

object QrCodeUtil {

    fun createQRImage(content: String, widthPix: Int, heightPix: Int, logoBm: Bitmap): Bitmap? {
        try {
            val hints = HashMap<EncodeHintType, Any>()
            hints[EncodeHintType.CHARACTER_SET] = StandardCharsets.UTF_8.name()
            hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.H
            hints[EncodeHintType.MARGIN] = 1
            var bitMatrix: BitMatrix? = null
            try {
                bitMatrix = QRCodeWriter().encode(
                    content, BarcodeFormat.QR_CODE, widthPix,
                    heightPix, hints
                )
            } catch (e: WriterException) {

            }
            val pixels = IntArray(widthPix * heightPix)
            for (y in 0 until heightPix) {
                for (x in 0 until widthPix) {
                    if (bitMatrix != null) {
                        if (bitMatrix.get(x, y)) {
                            pixels[y * widthPix + x] = -0x1000000
                        } else {
                            pixels[y * widthPix + x] = -0x1
                        }
                    } else {
                        return null
                    }
                }
            }
            var bitmap: Bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888)
            bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix)
            if (logoBm != null) {
                bitmap = addLogo(bitmap, logoBm)
            }
            return bitmap
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 在二维码中间添加Logo图案
     */
    fun addLogo(src: Bitmap, logo: Bitmap): Bitmap {

        //获取图片的宽高
        val srcWidth = src.getWidth();
        val srcHeight = src.getHeight();
        val logoWidth = logo.getWidth();
        val logoHeight = logo.getHeight();


        //logo大小为二维码整体大小的1/5
        val scaleFactor = srcWidth * 1.0f / 4 / logoWidth;
        val bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
        try {
            val canvas = Canvas(bitmap)
            canvas.drawBitmap(src, 0f, 0f, null)
            canvas.scale(scaleFactor, scaleFactor, srcWidth / 2f, srcHeight / 2f);
            canvas.drawBitmap(
                logo,
                (srcWidth - logoWidth) / 2f,
                (srcHeight - logoHeight) / 2f,
                null
            )
//            Canvas.ALL_SAVE_FLAG
            canvas.save()
            canvas.restore()
        } catch (e: Exception) {
            e.getStackTrace();
            return src;
        }

        return bitmap;
    }

}